package by.demchenko.sample.services.impl;

import by.demchenko.sample.dataaccess.base.CourseDao;
import by.demchenko.sample.datamodel.Course;
import by.demchenko.sample.datamodel.Person;
import by.demchenko.sample.datamodel.Schedule;
import by.demchenko.sample.datamodel.Section;
import by.demchenko.sample.datamodel.enums.ScheduleStatus;
import by.demchenko.sample.services.CourseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    private static final Logger LOGGER = LoggerFactory.getLogger(FacultyServiceImpl.class);

    @Inject
    private CourseDao dao;

    @Override
    public Course get(Integer id) {
        return dao.getById(id);
    }

    @Override
    public List<Course> getAll() {
        return dao.findAll();
    }

    @Override
    public void save(Course Course) {
        dao.insert(Course);
    }

    @Override
    public void saveOrUpdate(Course course) {
        if (course.getId() == null) {
            LOGGER.debug("Save new: {}", course);
            dao.insert(course);
        } else {
            LOGGER.debug("Update: {}", course);
            dao.update(course);
        }
    }

    @Override
    public void delete(Integer id) {
        dao.delete(id);
    }

    @Override
    public Integer getCount() {
        return dao.getCount();
    }

    @Override
    public List<Schedule> getAllCourseRegistrations() {
        return dao.getCourseToRegister();
    }

    @Override
    public List<Course> getCourseRegistrationBySection(Section section) {
        return dao.getCourseToRegister(section);
    }

    @Override
    public List<Course> getCourseRegistrationForTable(Person teacher) {
        return dao.getCourseForTable(teacher);
    }

    @Override
    public List<Course> getCourseRegistrationStatus(ScheduleStatus status) {
        return dao.getCourseRegistrationStatus(status);
    }

    @Override
    public List<Course> getAll(boolean fetchJoins) {
        return dao.findAll(fetchJoins);
    }

    @Override
    public List<Course> getAll(SingularAttribute<Course, ?> attr, boolean ascending, int startRecord, int pageSize, boolean fetchJoins) {
        return dao.findAll(attr, ascending, startRecord, pageSize, fetchJoins);
    }

    @Override
    public List<Course> getCoursesBySection(Section section) {
        return dao.getCourseBySection(section);
    }

    @Override
    public Course getByName(String name) {
        return dao.findByName(name);
    }
}